<% @nav = @blog.category %>
<% @title = @blog.title %>
<% @description = @blog.cached_tag_list.to_s + ' ' + @blog.content.truncate(200).gsub(/(\r|\n)/, ' ') %>

<% content_for :stylesheets do %>
<%= stylesheet_link_tag '/skins/default/github', 'hightlight/github.min' %>
<% end %>

<div class="left">
  
  <div class="con">
    
    <div class="content">
    	<div class="tit clearfix">
  			<%= blog_category_icon(@blog) %>
        <h1><%= link_to @blog.title, blog_url(@blog) %></h1>
        <%= link_to '', blog_url(@blog, mime_type = :md), :class => 'markdown', :title => 'Markdown格式原文' %>
        <% if account_admin? %>
        <%= link_to '', url(:admin, :blog, :id => @blog.id), :method => :delete, :confirm => '要删除文章吗？', :class => 'del', :title => '删除' %>
        <%= link_to '', url(:admin, :edit_blog, :id => @blog.id), :class => 'edit', :title => '编辑' %>
        <% end %>
      </div>
      <% unless @blog.cached_tags.blank? %>
      <div class="info clearfix">
        <% @blog.cached_tags.each do |tag| %>
        <%= link_to "<span>#{tag}</span>".html_safe, url(:blog, :tag, :name => tag), :class => 'tag', :rel => 'tag' %>
        <% end %>
      </div>
      <% end %>
      <div class="text">
        <%= @blog.md_content.html_safe %>
      </div>
      <div class="info clearfix">
        <div class="fr">
          <span class="author"><%= @blog.account.name %></span>
          <span class="time"><%= time_ago_in_words(@blog.created_at) %>发表</span>
          <span class="edit"><%= time_ago_in_words(@blog.content_updated_at) %>更新</span>
          <span class="views" title="浏览次数"><%= @blog.view_count %>次浏览</span>
        </div>
      </div>
    </div><!-- content -->
  
    <div class="share clearfix">
      <span><%= partial 'blog/weibo_share', :locals => {:content => (@blog.title + ":" + @blog.content).truncate(120).gsub(/\r|\n/, '')} %></span>
    </div><!-- share -->
    
    <div class="comment clearfix">
      <div id="comments">      
        <h2><span><%= @blog.comments_count %></span>条评论</h2>
        <ul>
          <% if @blog.comments_count > 0 %>
          <%= partial 'blog/comment', :collection => @blog.comments.reverse %>
          <% end %>
        </ul>
      </div>
      <% if @blog.commentable? %>
      <a name="comments"></a>
      <div class="relative">
        <div class="editor_switcher">
          <ul>
            <li class="current"><a id="publish_comment" href="#">发表评论</a></li>
            <li><a id="preview_comment" href="#">预览评论</a></li>
          </ul>
          <span style="float:right;">[<a href="http://wowubuntu.com/markdown/" target="_blank">评论可以使用Markdown格式</a>]</span>
        </div>
        <% form_for BlogComment.new, url(:blog, :create_comment, :id => @blog.id), :remote => true do |f| %>
          <%= f.text_area :content, :class => 'comment' %>
          <div id="comment-error-info"></div>
          <div id="comment_preview" class="con comment"></div>
          <%= f.submit '', :class => 'comment_btn' %>
        <% end %>
        <div class="box" style="<%= "display:none;" if account_login? %>">
          <button class="close"></button>
          <p>我想知道与谁交流思想，请用微博登录留言。</p>
          <button class="sina_btn"></button>
        </div><!-- box -->
      </div><!-- relative -->
      <% end %>
      
    </div><!-- comment -->
     
  </div><!-- con -->
  
</div><!-- left -->

<div class="right">
  <%= partial 'blog/right' %>
</div><!-- right -->

<% content_for :javascripts do %>
  <%= javascript_include_tag 'highlight.min' %>
  <script type="text/javascript">
  $(function(){
    // hightlight code
    hljs.initHighlightingOnLoad();

    // response close button of weibo login div
    $('button.close').click(function() {
      $('div.box').hide();
    });

    // response sina login button
    $('button.sina_btn').click(function(){
      var width=Math.round((window.screen.width-800)/2);
      var height=Math.round((window.screen.height-600)/2);
      window.open('<%= url(:weibo_login) %>/?quick_login=1', 'weibo_third_part_authentication', 'height=300px, width=400px, top='+height+',left='+width+', scrollbars=no, resizable=no, status=no, resizable=no, menubar=no');
    });
    
    // validate empty comment
    $('input.comment_btn').click(function(){
      if ($('textarea#blog_comment_content').val().length < 3) {
        $('div#comment-error-info').html('评论太少，多写点').show().fadeOut(3000);
        return false;
      }
    });
    
    // preview comments
    $('a#publish_comment').click(function(){
      $('a#publish_comment').parent().addClass('current');
      $('a#preview_comment').parent().removeClass('current');
      $('div#comment_preview').hide();
      $('textarea#blog_comment_content').show();
      return false;
    });
    $('a#preview_comment').click(function(){
      $.post('<%= url(:blog, :comment_preview) %>', {term: $('textarea#blog_comment_content').val()}, function(data){
        $('a#preview_comment').parent().addClass('current');
        $('a#publish_comment').parent().removeClass('current');
        $('textarea#blog_comment_content').hide();
        $('div#comment_preview').html(data);
        $('div#comment_preview').show();
      });
      return false;
    });

    // quote comment
    $('div.cot_con a.quote_comment').click(function(){
      var commentBlock = $(this).closest('li');
      $.get('<%= url(:blog, :quote_comment) %>', {id: commentBlock.attr('id')}, function(data){
        var body = $('textarea#blog_comment_content').val() + data;
        $('textarea#blog_comment_content').val(body);
        $('textarea#blog_comment_content').focus();
      });
      return false;
    });
  });
  </script>
<% end %>